module EStep {
    export class PluginService {
        private background: CordovaPluginBackgroundMode;
        private isReady: boolean;
        private device: Device;
        public networkState = {
            isOnline: false,
            isWifi:   false,
            ssid:     ''
        };
        private WifiWizard: WifiWizard;

        constructor() {
            document.addEventListener("offline", this.offline, false);
            document.addEventListener("online", this.online, false);
        }

        public IsReady(): boolean {
            return this.isReady;
        }

        public SetIsReady(isReady: boolean): void {
            console.log('SetIsReady:' + isReady);
            this.isReady = isReady;
        }

        public SetDevice(device: Device): void {
            this.device = device;
            console.log('device: ' + angular.toJson(device));
        }

        private offline = () => {
            const base = this;
            console.log('Network offline.');
            base.networkState.isOnline = false;
            base.networkState.isWifi   = false;
            base.networkState.ssid     = null;
        };

        public SetWifiWizard(WifiWizard) {
            this.WifiWizard = WifiWizard;
            const base      = this;
            if (this.networkState.isWifi) {
                this.WifiWizard.getCurrentSSID((ssid) => {
                    console.log(ssid);
                    base.networkState.ssid = ssid;
                }, (error) => {
                    console.log(angular.toJson(error));
                    base.networkState.ssid = angular.toJson(error);
                });
            }
        }

        private online = () => {
            const base = this;
            console.log('Network online. type:' + navigator.connection.type);
            console.log(angular.toJson(base.networkState));
            base.networkState.isOnline = true;
            base.networkState.isWifi   = navigator.connection.type.toUpperCase() == 'WIFI';
            if (base.WifiWizard) {
                base.WifiWizard.getCurrentSSID((ssid) => {
                    console.log(ssid);
                    base.networkState.ssid = ssid;
                }, (error) => {
                    console.log(angular.toJson(error));
                    base.networkState.ssid = angular.toJson(error);
                });
            }
        };

        public SetBackground(obj: CordovaPluginBackgroundMode) {
            if (obj) {
                console.log('初始化后台运行模式插件。');
                this.background              = obj;
                this.background.onactivate   = () => {
                    console.log("background.onactivate,isActive:" + this.background.isActive())
                };
                this.background.ondeactivate = () => {
                    console.log("background.ondeactivate,isActive:" + this.background.isActive())
                }
            }
            else {
                this.background = null;
                console.log('初始化后台运行模式插件。传入参数为null。');
            }

        }

        public EnableBackground(): void {
            if (this.background) {
                this.background.enable();
                console.log('background.isEnable:' + this.background.isEnabled());
            }
            else {
                console.log('plugin background is null.');
            }
        }

        public DisableBackground(): void {
            if (this.background) {
                this.background.disable();
                console.log('background.isEnable:' + this.background.isEnabled());
            }
            else {
                console.log('plugin background is null.');
            }
        }
    }

    angular.module('EStep')
        .service('PluginService', PluginService);
}
